document.addEventListener('DOMContentLoaded', function() {
    // 创建滚动容器
    const content = document.body.innerHTML;
    document.body.innerHTML = `<div class="smooth-scroll-container">${content}</div>`;
    const container = document.querySelector('.smooth-scroll-container');
    
    let isScrolling = false;
    let scrollTimeout;
    let currentPosition = 0;
    const scrollStep = 100; // 每次滚动的步长
    
    // 处理鼠标滚轮事件
    function handleWheel(e) {
        e.preventDefault();
        
        if (!isScrolling) {
            isScrolling = true;
            
            // 确定滚动方向
            const direction = e.deltaY > 0 ? 1 : -1;
            currentPosition = container.scrollTop;
            const targetPosition = currentPosition + (direction * scrollStep);
            
            // 平滑滚动到目标位置
            container.scrollTo({
                top: targetPosition,
                behavior: 'smooth'
            });
            
            // 设置滚动冷却时间
            clearTimeout(scrollTimeout);
            scrollTimeout = setTimeout(() => {
                isScrolling = false;
            }, 500); // 滚动冷却时间，可以根据需要调整
        }
    }
    
    // 处理触摸事件
    let touchStartY = 0;
    let touchEndY = 0;
    
    function handleTouchStart(e) {
        touchStartY = e.touches[0].clientY;
    }
    
    function handleTouchMove(e) {
        e.preventDefault();
        touchEndY = e.touches[0].clientY;
        
        if (!isScrolling) {
            isScrolling = true;
            
            const direction = touchStartY > touchEndY ? 1 : -1;
            currentPosition = container.scrollTop;
            const targetPosition = currentPosition + (direction * scrollStep);
            
            container.scrollTo({
                top: targetPosition,
                behavior: 'smooth'
            });
            
            clearTimeout(scrollTimeout);
            scrollTimeout = setTimeout(() => {
                isScrolling = false;
            }, 500);
            
            touchStartY = touchEndY;
        }
    }
    
    // 添加事件监听器
    container.addEventListener('wheel', handleWheel, { passive: false });
    container.addEventListener('touchstart', handleTouchStart);
    container.addEventListener('touchmove', handleTouchMove, { passive: false });
    
    // 处理键盘事件
    document.addEventListener('keydown', function(e) {
        if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
            e.preventDefault();
            
            if (!isScrolling) {
                isScrolling = true;
                
                const direction = e.key === 'ArrowDown' ? 1 : -1;
                currentPosition = container.scrollTop;
                const targetPosition = currentPosition + (direction * scrollStep);
                
                container.scrollTo({
                    top: targetPosition,
                    behavior: 'smooth'
                });
                
                clearTimeout(scrollTimeout);
                scrollTimeout = setTimeout(() => {
                    isScrolling = false;
                }, 500);
            }
        }
    });
}); 